import 'package:drift/drift.dart' as db;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo_k/view/custom_app_bar.dart';

import '../change_notifier/drift_notifier/drift_book.dart';
import '../change_notifier/notifier_provider.dart';
import '../data_base/user_db.dart';

class DriftTest extends StatefulWidget {
  static const String routeName = '/drift_test';
  const DriftTest({super.key});

  @override
  State<DriftTest> createState() => _DriftTestState();
}

class _DriftTestState extends State<DriftTest> {
  DriftBook provider = DriftBook();
  AppDatabase database = AppDatabase.instance;
  late Future<List<User>> _usersFuture;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    provider.init();
    _usersFuture = database.getAllUsers();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: '数据库测试',
      ),
      body: NotifierProvider(
        notifier: provider,
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 10),
          child: Column(
            children: [
              GestureDetector(
                onTap: () => {
                  printAllUsers(database),
                },
                child: Container(
                  height: 20,
                  color: Colors.blue,
                  child: const Text('打印数据'),
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              GestureDetector(
                onTap: () => {
                  printAllUsers(database),
                },
                child: Container(
                  height: 20,
                  color: Colors.blue,
                  child: const Text('删除数据'),
                ),
              ),
              Expanded(
                child: FutureBuilder<List<User>>(
                  future: _usersFuture,
                  builder: (BuildContext context,
                      AsyncSnapshot<List<User>> snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return const Center(child: CircularProgressIndicator());
                    } else if (snapshot.hasError) {
                      return Center(child: Text('加载数据时出错: ${snapshot.error}'));
                    } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
                      return const Center(child: Text('没有用户数据'));
                    }
                    final users = snapshot.data!;
                    return ListView.builder(
                      itemCount: users.length,
                      itemBuilder: (context, index) {
                        final user = users[index];
                        return ListTile(
                          leading: Text('${user.id}'),
                          title: Text(user.name),
                          subtitle: Text('年龄: ${user.age}'),
                        );
                      },
                    );
                  },
                ),
              )
            ],
          ),
        ),
      ),
      floatingActionButton: GestureDetector(
        onTap: () => {
          database.insertUser(
            const UsersCompanion(
              name: db.Value('krcm110'),
              age: db.Value(10),
            ),
          ),
        },
        child: Container(
          child: Container(
            decoration: const BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.all(
                Radius.circular(10),
              ),
            ),
            width: 140,
            height: 30,
            child: const Center(
              child: Text('插入数据'),
            ),
          ),
        ),
      ),
    );
  }
}
